<?php
    class Admin_CommentController extends GTL_Action {
         public function init() {
            parent::init();
            /* Initialize action controller here */
            $this->list_sess_unset('controller_comment');
            $this->session_search_key = 'controller_comment';
            $this->session_search_name = 'search_label';
        }
        public function indexAction() {
            $this->_helper->redirector('list', 'comment','admin');
        }

        public function viewAction() {
            if ($this->_getParam('cmt_id') && $this->_getParam('cmt_id') != '') {
                $cmt_id = $this->_getParam('cmt_id');
            } else {
                $this->_helper->redirector('list', 'comment', 'admin');
            }
            $commentModel = new Model_Comment();
            $commentalldata = $commentModel->fetchEntryById($cmt_id);
            $this->view->commentalldata = $commentalldata;
        }
        public function listAction() {
           
            $errorMessage= Array();
            $successmessage = '';
            $where = '';
            $searchtype = '';
            $orderField = 'cmt_id';
            $sort = 'asc';


            /* Pagination Login */
            $itemsPerPageReviews = $this->PAGING_PER_PAGE;
            $currentPageReviews = 1;
            //PAGING_PER_PAGE
            
            if ($this->_getParam('blog_id') && $this->_getParam('blog_id') != '') {
                $blog_id = $this->_getParam('blog_id');
            }else{
                $this->_helper->redirector('list', 'blog', 'admin');
            }
            
            if ($this->_getParam('page') && $this->_getParam('page') != '') {
                $currentPageReviews = $this->_getParam('page');
            }

            if ($this->_getParam('field') && $this->_getParam('field') != '') {
                $orderField = $this->_getParam('field');
            }

            if ($this->_getParam('sort') && $this->_getParam('sort') != '') {
                $sort = $this->_getParam('sort');
            }
            $offset = ($currentPageReviews - 1) * $itemsPerPageReviews;

            if ($this->_request->isPost()) {
                $postedData = $this->_request->getPost();
                //Multiple Delete Logic 
                if (isset($postedData['multiaction']) && ($postedData['multiaction'] != '')) {
                    $action = trim($postedData ['multiaction']);
                    if (isset($postedData['multicheck']) && count($postedData['multicheck']) > 0) {
                        $ids = implode(',', $_POST['multicheck']);
                        $result = $this->deleteAction($_POST['multicheck']);
                    } else {
                        $errorMessage[] = ' Please select atleast one checkbox! ';
                    }
                }
                /*search code here*/
                if ($postedData['searchtype'] == '') {
                    $_SESSION['_LISTING_SESS'][$this->session_search_key][$this->session_search_name] = '';
                }
                if (isset($postedData['searchtype']) && ($postedData['searchtype'] != '')) {
                    $searchtype = $postedData['searchtype'];
                    $where = " cmt_name LIKE '%$searchtype%'   OR cmt_phone_no LIKE '%$searchtype%'  OR cmt_note LIKE '%$searchtype%'  OR cmt_status LIKE '%$searchtype%' ";    
                    $_SESSION['_LISTING_SESS'][$this->session_search_key][$this->session_search_name] = $searchtype;
                    $this->view->searchtype = $searchtype;
			      
                }    
                
            } else {

                if (!isset($_SESSION['_LISTING_SESS'][$this->session_search_key][$this->session_search_name])) 
                    {
                    $_SESSION['_LISTING_SESS'][$this->session_search_key][$this->session_search_name] = '';
                }
                    $searchtype = $_SESSION['_LISTING_SESS'][$this->session_search_key][$this->session_search_name];
                    $where = " cmt_name LIKE '%$searchtype%'   OR cmt_phone_no LIKE '%$searchtype%'  OR cmt_note LIKE '%$searchtype%'  OR cmt_status LIKE '%$searchtype%' ";    
                    $_SESSION['_LISTING_SESS'][$this->session_search_key][$this->session_search_name] = $searchtype;
                    $this->view->searchtype = $searchtype;
            }
            $where = "cmt_blog_id = ".$blog_id;
            $commentModel = new Model_Comment();
            $commentsData = $commentModel->fetchComment($where, $itemsPerPageReviews, $offset, $orderField, $sort);
            $paginator = new GTL_Paginator();
            $paginator->setItemsPerPage($itemsPerPageReviews);
            $paginator->setItemsTotal($commentModel->getCommentCount($where));
            $paginator->setCurrentPage($currentPageReviews);
            $link = $this->view->url(array("module" => "admin",'controller' => 'comment', 'action' => 'list', 'page' => 'PAGENO'), null, false);
            $paginator->paginate($link);

            $this->view->paginator = $paginator;
            $this->view->commentsData = $commentsData;
            $this->view->errorMessage = $errorMessage;
            $this->view->successmessage = $successmessage;
            $this->view->statusArray = $commentModel->getStatusArray();
            $this->view->searchtype = $searchtype;
        }public function deleteAction($ids = Array()) {
            //Load comment Model 
            $commentModel = new Model_Comment();
            if ($this->_getParam('cmt_id') && $this->_getParam('cmt_id') != '') {
                $cmt_id = $this->_getParam('cmt_id');
                $commentModel->deleteComment($cmt_id);
                $this->addSuccessMessage('Record Successfully Deleted');
               $this->_helper->redirector('list', 'comment', 'admin');
            } else {
                $commentModel->deleteComment($ids);
               $this->addSuccessMessage('Record Successfully Deleted');
                $this->_helper->redirector('list', 'comment', 'admin');
            }
        }
     public function formAction() {
     //Initialize
            $successmessage = '';
            $errorMessageArray = Array();
            $data = array();
            $commentData = Array();
            $this->view->mode = 'Add';

            //Load comment Model
            $commentModel = new Model_Comment();

            //Load comment Form
            $commentForm = new Form_admin_CommentForm();

            //Load the status array for the select box in form 
            $statusArray = $commentModel->getStatusArray();
            $commentForm->cmt_status->addMultiOptions($statusArray);

            //Check for the comment Parameter
            $commentData['cmt_id'] = $this->_request->getParam('cmt_id');
            $cmt_id = (!empty($commentData['cmt_id'])) ? $commentData['cmt_id'] : null;
            if (!empty($cmt_id)) {
            //Set the Mode to Edit
            $this->view->mode = 'Edit';
            //Fetch data to be loaded in form 
            $data = $commentModel->fetchEntryById($cmt_id);
            }
            //Populate form 
            $this->view->form = $commentForm->populate($data);
            if ($this->getRequest()->isPost()) {
                //Check for Errors
                $postedData = $this->_request->getPost();
                if (!$commentForm->isValid($postedData)) {
                    $errorMessage = $commentForm->getMessages();
                    foreach ($errorMessage as $_err) {
                        foreach ($_err as $_val) {
                            $errorMessageArray[] = $_val;
                        }
                    }
                } else {
                
                        
                    if (count($errorMessageArray) > 0) {

                    } else {
                    
                          
                        if ($this->view->mode == 'Add') {
                            $commentData['cmt_createddate'] = date('Y-m-d H:i:s');
                        }
                        //comment save
                        $commentData['cmt_id'] = $cmt_id;
                        $commentData['cmt_blog_id'] = $postedData['cmt_blog_id'];
						$commentData['cmt_name'] = $postedData['cmt_name'];
						$commentData['cmt_phone_no'] = $postedData['cmt_phone_no'];
						$commentData['cmt_note'] = $postedData['cmt_note'];
						$commentData['cmt_updateddate'] = date('Y-m-d H:i:s');
						$commentData['cmt_status'] = $postedData['cmt_status'];
						

                        $commentModel->save($commentData, 'cmt_id');
                        if ($this->view->mode == 'Add') {
                            $successmessage = 'Comment add successfully';
                        } else {
                            $successmessage = 'Comment updated successfully';
                        }
                        $this->addSuccessMessage($successmessage);
                        $this->_helper->redirector('list', 'comment', 'admin');
                    }
                }
            }
            $this->view->commentid = $cmt_id;
            $this->view->errorMessageArray = $errorMessageArray;
            $this->view->succesMessage = $successmessage;
            $this->view->form = $commentForm;


    }
    }?>